package com.un.ebs;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author:zc
 * @Date:2020/12/17
 * @Description:程序启动类
 */
@SpringBootApplication
@MapperScan("com.un.ebs.sys.mapper")
@MapperScan("com.un.ebs.customer.mapper")
@MapperScan("com.un.ebs.order.mapper")
@MapperScan("com.un.ebs.product.mapper")
@MapperScan("com.un.ebs.workOrder.mapper")
@MapperScan("com.un.ebs.sale.mapper")
@MapperScan("com.un.ebs.production.mapper")
@MapperScan("com.un.ebs.purchase.mapper")
@MapperScan("com.un.ebs.inventory.mapper")
@MapperScan("com.un.ebs.quality.mapper")
@MapperScan("com.un.ebs.invoice.mapper")
@MapperScan("com.un.ebs.memorial.mapper")
@MapperScan("com.un.ebs.contract.mapper")
@MapperScan("com.un.ebs.workflow.mapper")
@MapperScan("com.un.ebs.context.mapper")
@MapperScan("com.un.ebs.userdetails.mapper")
@ComponentScan(basePackages = {"me.chanjar.weixin.mp.api", "com.un.ebs"})
@EnableScheduling
@EnableAspectJAutoProxy
@Slf4j
public class EbsApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(EbsApplication.class);
        Environment env = app.run(args).getEnvironment();
        try {
            log.info(
                    "\n----------------------------------------------------------\n\t"
                            + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\thttp://localhost:{}\n\t"
                            + "External: \thttp://{}:{}\n\t"
                            + "Profile(s): \t{}\n----------------------------------------------------------",
                    env.getProperty("spring.application.name"), env.getProperty("server.port"),
                    InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"),
                    env.getActiveProfiles());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    /**
     * 提供跨域支持
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean corsFilter() {
        org.springframework.web.cors.UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);

        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new CorsFilter(source));
        filterRegistrationBean.setOrder(0);
        return filterRegistrationBean;
    }
}
